home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util4 / bytmrk20.lha / nbench1.h < prev    next >
C/C++ Source or Header  |  1995-11-03  |  10KB  |  401 lines

  1.  
  2. /***********************
  3. ** COMMON DEFINITIONS **
  4. ***********************/
  5.  
  6. /*
  7. ** DEFINES
  8. */
  9. /* #define DEBUG */
  10.  
  11. /*
  12. ** EXTERNALS
  13. */
  14. extern ulong global_min_ticks;
  15.  
  16. extern SortStruct global_numsortstruct;
  17. extern SortStruct global_strsortstruct;
  18. extern BitOpStruct global_bitopstruct;
  19. extern EmFloatStruct global_emfloatstruct;
  20. extern FourierStruct global_fourierstruct;
  21. extern AssignStruct global_assignstruct;
  22. extern IDEAStruct global_ideastruct;
  23. extern HuffStruct global_huffstruct;
  24. extern NNetStruct global_nnetstruct;
  25. extern LUStruct global_lustruct;
  26.  
  27. /* External PROTOTYPES */
  28. extern unsigned long abs_randwc(unsigned long num);    /* From MISC */
  29. extern long randnum(long lngval);
  30.  
  31. extern farvoid *AllocateMemory(unsigned long nbytes,    /* From SYSSPEC */
  32.     int *errorcode);
  33. extern void FreeMemory(farvoid *mempointer,
  34.     int *errorcode);
  35. extern void MoveMemory(farvoid *destination,
  36.         farvoid *source, unsigned long nbytes);
  37. extern void ReportError(char *context, int errorcode);
  38. extern void ErrorExit(void);
  39. extern unsigned long StartStopwatch(void);
  40. extern unsigned long StopStopwatch(unsigned long startticks);
  41. extern unsigned long TicksToSecs(unsigned long tickamount);
  42. extern double TicksToFracSecs(unsigned long tickamount);
  43.  
  44. /*****************
  45. ** NUMERIC SORT **
  46. *****************/
  47.  
  48. /*
  49. ** PROTOTYPES
  50. */
  51. void DoNumSort(void);
  52. static ulong DoNumSortIteration(farlong *arraybase,
  53.         ulong arraysize,
  54.         uint numarrays);
  55. static void LoadNumArrayWithRand(farlong *array,
  56.         ulong arraysize,
  57.         uint numarrays);
  58. static void NumHeapSort(farlong *array,
  59.         ulong bottom,
  60.         ulong top);
  61. static void NumSift(farlong *array,
  62.         ulong i,
  63.         ulong j);
  64.  
  65.  
  66. /****************
  67. ** STRING SORT **
  68. *****************
  69. */
  70.  
  71.  
  72. /*
  73. ** PROTOTYPES
  74. */
  75. void DoStringSort(void);
  76. static ulong DoStringSortIteration(faruchar *arraybase,
  77.         uint numarrays,
  78.         ulong arraysize);
  79. static farulong *LoadStringArray(faruchar *strarray,
  80.         uint numarrays,
  81.         ulong *strings,
  82.         ulong arraysize);
  83. static void stradjust(farulong *optrarray,
  84.         faruchar *strarray,
  85.         ulong nstrings,
  86.         ulong i,
  87.         uchar l);
  88. static void StrHeapSort(farulong *optrarray,
  89.         faruchar *strarray,
  90.         ulong numstrings,
  91.         ulong bottom,
  92.         ulong top);
  93. static int str_is_less(farulong *optrarray,
  94.         faruchar *strarray,
  95.         ulong numstrings,
  96.         ulong a,
  97.         ulong b);
  98. static void strsift(farulong *optrarray,
  99.         faruchar *strarray,
  100.         ulong numstrings,
  101.         ulong i,
  102.         ulong j);
  103.  
  104. /************************
  105. ** BITFIELD OPERATIONS **
  106. *************************
  107. */
  108.  
  109. /*
  110. ** PROTOTYPES
  111. */
  112. void DoBitops(void);
  113. static ulong DoBitfieldIteration(farulong *bitarraybase,
  114.         farulong *bitoparraybase,
  115.         long bitoparraysize,
  116.         ulong *nbitops);
  117. static void ToggleBitRun(farulong *bitmap,
  118.         ulong bit_addr,
  119.         ulong nbits,
  120.         uint val);
  121. static void FlipBitRun(farulong *bitmap,
  122.         ulong bit_addr,
  123.         ulong nbits);
  124.  
  125. /****************************
  126. ** EMULATED FLOATING POINT **
  127. ****************************/
  128. typedef struct
  129. {
  130.     u8 type;    /* Indicates, NORMAL, SUBNORMAL, etc. */
  131.     u8 sign;    /* Mantissa sign */
  132.     short exp;    /* Signed exponent...no bias */
  133.     u16 mantissa[INTERNAL_FPF_PRECISION];
  134. } InternalFPF;
  135.  
  136. /*
  137. ** PROTOTYPES
  138. */
  139. void DoEmFloat(void);
  140.  
  141. /*
  142. ** EXTERNALS
  143. */
  144. extern void SetupCPUEmFloatArrays(InternalFPF *abase,
  145.     InternalFPF *bbase, InternalFPF *cbase,
  146.     ulong arraysize);
  147. extern ulong DoEmFloatIteration(InternalFPF *abase,
  148.     InternalFPF *bbase, InternalFPF *cbase,
  149.     ulong arraysize, ulong loops);
  150.  
  151. /*************************
  152. ** FOURIER COEFFICIENTS **
  153. *************************/
  154.  
  155. /*
  156. ** PROTOTYPES
  157. */
  158. void DoFourier(void);
  159. static ulong DoFPUTransIteration(fardouble *abase,
  160.         fardouble *bbase,
  161.         ulong arraysize);
  162. static double TrapezoidIntegrate(double x0,
  163.         double x1,
  164.         int nsteps,
  165.         double omegan,
  166.         int select);
  167. static double thefunction(double x,
  168.         double omegan,
  169.         int select);
  170.  
  171. /*************************
  172. ** ASSIGNMENT ALGORITHM **
  173. *************************/
  174.  
  175. /*
  176. ** DEFINES
  177. */
  178.  
  179. #define ASSIGNROWS 101
  180. #define ASSIGNCOLS 101
  181.  
  182. /*
  183. ** TYPEDEFS
  184. */
  185. typedef struct {
  186.     union {
  187.         long *p;
  188.         long (*ap)[ASSIGNROWS][ASSIGNCOLS];
  189.     } ptrs;
  190. } longptr;
  191.  
  192. /*
  193. ** PROTOTYPES
  194. */
  195. void DoAssign(void);
  196. static ulong DoAssignIteration(farlong *arraybase,
  197.         ulong numarrays);
  198. static void LoadAssignArrayWithRand(farlong *arraybase,
  199.         ulong numarrays);
  200. static void LoadAssign(farlong arraybase[][ASSIGNCOLS]);
  201. static void CopyToAssign(farlong arrayfrom[][ASSIGNCOLS],
  202.         long arrayto[][ASSIGNCOLS]);
  203. static void Assignment(farlong arraybase[][ASSIGNCOLS]);
  204. static void calc_minimum_costs(long tableau[][ASSIGNCOLS]);
  205. static int first_assignments(long tableau[][ASSIGNCOLS],
  206.         short assignedtableau[][ASSIGNCOLS]);
  207. static void second_assignments(long tableau[][ASSIGNCOLS],
  208.         short assignedtableau[][ASSIGNCOLS]);
  209.  
  210. /********************
  211. ** IDEA ENCRYPTION **
  212. ********************/
  213.  
  214. /*
  215. ** DEFINES
  216. */
  217. #define IDEAKEYSIZE 16
  218. #define IDEABLOCKSIZE 8
  219. #define ROUNDS 8
  220. #define KEYLEN (6*ROUNDS+4)
  221.  
  222. /*
  223. ** MACROS
  224. */
  225. #define low16(x) ((x) & 0x0FFFF)
  226. #define MUL(x,y) (x=mul(low16(x),y))
  227.  
  228.  
  229. typedef u16 IDEAkey[KEYLEN];
  230.  
  231. /*
  232. ** PROTOTYPES
  233. */
  234. void DoIDEA(void);
  235. static ulong DoIDEAIteration(faruchar *plain1,
  236.     faruchar *crypt1, faruchar *plain2,
  237.     ulong arraysize, ulong nloops,
  238.     IDEAkey Z, IDEAkey DK);
  239. static u16 mul(register u16 a, register u16 b);
  240. static u16 inv(u16 x);
  241. static void en_key_idea(u16 userkey[8], IDEAkey Z);
  242. static void de_key_idea(IDEAkey Z, IDEAkey DK);
  243. static void cipher_idea(u16 in[4], u16 out[4], IDEAkey Z);
  244.  
  245. /************************
  246. ** HUFFMAN COMPRESSION **
  247. ************************/
  248.  
  249. /*
  250. ** DEFINES
  251. */
  252. #define EXCLUDED 32000          /* Big positive value */
  253.  
  254. /*
  255. ** TYPEDEFS
  256. */
  257. typedef struct {
  258.         uchar c;        /* Byte value */
  259.         float freq;        /* Frequency */
  260.         int parent;        /* Parent node */
  261.         int left;        /* Left pointer = 0 */
  262.         int right;        /* Right pointer = 1 */
  263. } huff_node;
  264.  
  265. /*
  266. ** GLOBALS
  267. */
  268. static huff_node *hufftree;             /* The huffman tree */
  269. static long plaintextlen;               /* Length of plaintext */
  270.  
  271. /*
  272. ** PROTOTYPES
  273. */
  274. void DoHuffman(void);
  275. static void create_text_line(farchar *dt,long nchars);
  276. static void create_text_block(farchar *tb, ulong tblen,
  277.         ushort maxlinlen);
  278. static ulong DoHuffIteration(farchar *plaintext,
  279.     farchar *comparray, farchar *decomparray,
  280.     ulong arraysize, ulong nloops, huff_node *hufftree);
  281. static void SetCompBit(u8 *comparray, u32 bitoffset, char bitchar);
  282. static int GetCompBit(u8 *comparray, u32 bitoffset);
  283.  
  284. /********************************
  285. ** BACK PROPAGATION NEURAL NET **
  286. ********************************/
  287.  
  288. /*
  289. ** DEFINES
  290. */
  291. #define T 1            /* TRUE */
  292. #define F 0            /* FALSE */
  293. #define ERR -1
  294. #define MAXPATS 10        /* max number of patterns in data file */
  295. #define IN_X_SIZE 5        /* number of neurodes/row of input layer */
  296. #define IN_Y_SIZE 7        /* number of neurodes/col of input layer */
  297. #define IN_SIZE 35        /* equals IN_X_SIZE*IN_Y_SIZE */
  298. #define MID_SIZE 8        /* number of neurodes in middle layer */
  299. #define OUT_SIZE 8        /* number of neurodes in output layer */
  300. #define MARGIN 0.1        /* how near to 1,0 do we have to come to stop? */
  301. #define BETA 0.09        /* beta learning constant */
  302. #define ALPHA 0.09        /* momentum term constant */
  303. #define STOP 0.1        /* when worst_error less than STOP, training is done */
  304.  
  305. /*
  306. ** GLOBALS
  307. */
  308. double  mid_wts[MID_SIZE][IN_SIZE];     /* middle layer weights */
  309. double  out_wts[OUT_SIZE][MID_SIZE];    /* output layer weights */
  310. double  mid_out[MID_SIZE];              /* middle layer output */
  311. double  out_out[OUT_SIZE];              /* output layer output */
  312. double  mid_error[MID_SIZE];            /* middle layer errors */
  313. double  out_error[OUT_SIZE];            /* output layer errors */
  314. double  mid_wt_change[MID_SIZE][IN_SIZE]; /* storage for last wt change */
  315. double  out_wt_change[OUT_SIZE][MID_SIZE]; /* storage for last wt change */
  316. double  in_pats[MAXPATS][IN_SIZE];      /* input patterns */
  317. double  out_pats[MAXPATS][OUT_SIZE];    /* desired output patterns */
  318. double  tot_out_error[MAXPATS];         /* measure of whether net is done */
  319. double  out_wt_cum_change[OUT_SIZE][MID_SIZE]; /* accumulated wt changes */
  320. double  mid_wt_cum_change[MID_SIZE][IN_SIZE];  /* accumulated wt changes */
  321.  
  322. double  worst_error; /* worst error each pass through the data */
  323. double  average_error; /* average error each pass through the data */
  324. double  avg_out_error[MAXPATS]; /* average error each pattern */
  325.  
  326. int iteration_count;    /* number of passes thru network so far */
  327. int numpats;            /* number of patterns in data file */
  328. int numpasses;          /* number of training passes through data file */
  329. int learned;            /* flag--if TRUE, network has learned all patterns */
  330.  
  331. /*
  332. ** The Neural Net test requires an input data file.
  333. ** The name is specified here.
  334. */
  335. char *inpath="NNET.DAT";
  336.  
  337. /*
  338. ** PROTOTYPES
  339. */
  340. void DoNNET(void);
  341. static ulong DoNNetIteration(ulong nloops);
  342. static void do_mid_forward(int patt);
  343. static void do_out_forward(void);
  344. void display_output(int patt);
  345. static void do_forward_pass(int patt);
  346. static void do_out_error(int patt);
  347. static void worst_pass_error(void);
  348. static void do_mid_error(void);
  349. static void adjust_out_wts(void);
  350. static void adjust_mid_wts(int patt);
  351. static void do_back_pass(int patt);
  352. static void move_wt_changes(void);
  353. static int check_out_error(void);
  354. static void zero_changes(void);
  355. static void randomize_wts(void);
  356. static int read_data_file(void);
  357. static int initialize_net(void);
  358.  
  359. /***********************
  360. **  LU DECOMPOSITION  **
  361. ** (Linear Equations) **
  362. ***********************/
  363.  
  364. /*
  365. ** DEFINES
  366. */
  367.  
  368. #define LUARRAYROWS 101
  369. #define LUARRAYCOLS 101
  370.  
  371. /*
  372. ** TYPEDEFS
  373. */
  374. typedef struct
  375. {    union
  376.     {    double *p;
  377.         double (*ap)[LUARRAYROWS][LUARRAYCOLS];
  378.     } ptrs;
  379. } LUdblptr;
  380.  
  381. /*
  382. ** PROTOTYPES
  383. */
  384. void DoLU(void);
  385. static void LUFreeMem(double *a, double *b,
  386.     double *abase, double *bbase);
  387. static ulong DoLUIteration(double *a, double *b,
  388.     double *abase, double *bbase,
  389.     ulong numarrays);
  390. static void build_problem(double a[][LUARRAYCOLS],
  391.     int n, double b[LUARRAYROWS]);
  392. static int ludcmp(double a[][LUARRAYCOLS],
  393.     int n, int indx[], int *d);
  394. static void lubksb(double a[][LUARRAYCOLS],
  395.     int n, int indx[LUARRAYROWS],
  396.     double b[LUARRAYROWS]);
  397. static int lusolve(double a[][LUARRAYCOLS],
  398.     int n, double b[LUARRAYROWS]);
  399.  
  400.  
  401.